﻿Imports System
Imports System.IO
Imports System.Data
Imports System.Text
Imports System.Data.SqlClient
Imports Infragistics
Imports Infragistics.Win
Imports Infragistics.Shared

Public Class Frm_SYSUserpurview
    Inherits System.Windows.Forms.UserControl


    Implements IDServer.IDServer.IDServer


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp
    Private mDataSet As DataSet
    Private m_DS As DataSet
    Private GridDs As DataSet
    Public bFind As Boolean
    Private SelectNode As TreeNode
    Private Img As ImageList
    'Private ModeName As String
    Private WithEvents MainForm As Frm_MainForm


    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

#Region "sub or fucntion"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Tv"></param>
    ''' <remarks></remarks>
    Private Sub LoadTreeView(ByVal Tv As TreeView)
        Try
            Dim mNode As New TreeNode
            Img = New ImageList()
            Dim Company As String = Application.StartupPath & "\pic\35FLOPPY.BMP"
            Dim bumen As String = Application.StartupPath & "\pic\CLSDFOLD.BMP"
            Dim gangwei As String = Application.StartupPath & "\pic\OPENFOLD.BMP"
            Dim Image As Image = Nothing
            Img.Images.Add(Drawing.Image.FromFile(Company))
            Img.Images.Add(Drawing.Image.FromFile(bumen))
            Img.Images.Add(Drawing.Image.FromFile(gangwei))
            Tv.ImageList = Img
            mNode.Nodes.Clear()
            mNode.Tag = "All"
            mNode.Text = "全部"
            mNode.ImageIndex = 0
            Tv.Nodes.Add(mNode)
            LoadSecondTree(mNode)
            Tv.SelectedNode = mNode
            mNode.Expand()
        Catch ex As Exception
            MsgBox("LoadTreeView:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Nodes"></param>
    ''' <remarks></remarks>
    Private Sub LoadSecondTree(ByVal Nodes As TreeNode)
        Try
            Dim sql As String = ""
            sql = "select GroupCode,GroupName,GroupStartDate,GroupEndDate,GroupStatus,GroupRemark from dbo.Sys_Group"
            mDataSet = New DataSet
            mDataSet = ReturnDataset(sql)
            If Not mDataSet Is Nothing Then
                For Each mRow As DataRow In mDataSet.Tables(0).Rows
                    Dim Node1 As New TreeNode
                    Node1.Tag = mRow("GroupCode")
                    Node1.Text = mRow("GroupName")
                    Node1.ImageIndex = 1
                    Node1.SelectedImageIndex = 2
                    Nodes.Nodes.Add(Node1)
                Next
            End If
        Catch ex As Exception
            MsgBox("LoadSecondTree:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="UTree"></param>
    ''' <remarks></remarks>
    Private Sub LoadUTree(ByVal UTree As TreeView)
        Try
            Dim sql As String = ""
            Dim mNode As TreeNode
            mNode = New TreeNode
            Img = New ImageList()
            Dim Pictures As String = "35FLOPPY.BMP,Company.png,Company1.png,Depart.png,Depart1.png,Position.png,Position1.png"
            Dim Str() As String = Pictures.Split(",")
            Dim Company As String = Application.StartupPath & "\pic\35FLOPPY.BMP"
            Dim bumen As String = Application.StartupPath & "\pic\CLSDFOLD.BMP"
            Dim gangwei As String = Application.StartupPath & "\pic\OPENFOLD.BMP"
            Dim Image As Image = Nothing
            For i As Integer = 0 To Str.Length - 1
                Img.Images.Add(Drawing.Image.FromFile(Application.StartupPath & "\pic\" & Str(i)))
            Next
            UTree.ImageList = Img
            mNode.Nodes.Clear()
            mNode.Tag = "All"
            mNode.Text = "全部"
            mNode.ImageIndex = 0
            UTree.Nodes.Add(mNode)
            sql = "select * from Sys_ModeDefine"
            m_DS = ReturnDataset(sql)
            LoadSecondTree(mNode, 0)
            SelectNode = mNode
            UTree.SelectedNode = SelectNode
            SelectNode.Expand()
            UTree.CheckBoxes = True
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Node"></param>
    ''' <param name="ParentID"></param>
    ''' <remarks></remarks>
    Private Sub LoadSecondTree(ByVal Node As TreeNode, ByVal ParentID As Integer)
        Try
            If mDataset Is Nothing Then Exit Sub
            Dim mRow() As DataRow = m_DS.Tables(0).Select("ParentID=" & ParentID)

            If mRow.Length > 0 Then
                For Each Rows As DataRow In mRow
                    Dim Node1 As New TreeNode
                    If Node Is Nothing Then
                        Node1.Tag = Rows("ControlName")
                        Node1.Text = Rows("ModeName")
                        Node1.ImageIndex = 0
                        Node.Nodes.Add(Node1)
                        Node.Expand()
                        LoadSecondTree(Node1, Rows("ID"))
                    Else
                        Node1.Tag = Rows("ControlName")
                        Node1.Text = Rows("ModeName")
                        If ParentID = 0 Then
                            Node1.ImageIndex = 1
                            Node1.SelectedImageIndex = 2
                        Else
                            Node1.ImageIndex = 3
                            Node1.SelectedImageIndex = 4
                        End If
                        Node.Nodes.Add(Node1)
                        Node.Expand()
                        LoadSecondTree(Node1, Rows("ID"))
                    End If

                Next
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ClearText()
        Try
            Me.txtGroupNo.Text = ""
            Me.txtGroupName.Text = ""
            Me.txtRemark.Text = ""
            Me.CBStatus.Checked = False
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="B"></param>
    ''' <remarks></remarks>
    Private Sub SetEnable(ByVal B As Boolean)
        Try
            Me.txtGroupNo.Enabled = B
            Me.txtGroupName.Enabled = B
            Me.txtStartDate.Enabled = B
            Me.txtEndDate.Enabled = B
            Me.txtRemark.ReadOnly = Not B
            Me.BtnOK.Enabled = B
            Me.BtnCancel.Enabled = B
            Me.TreeView1.Enabled = Not B
            Me.CBStatus.Enabled = B
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ReturnTable(ByVal GroupID As String)
        Dim nTable As New DataTable
        Try
            With nTable
                .Columns.Add("GroupID")
                .Columns.Add("ModeID")
                .Columns.Add("ActionSee", GetType(Boolean))
                .Columns.Add("ActionEnable", GetType(Boolean))
                .Columns.Add("ActionBrowser", GetType(Boolean))
                .Columns.Add("ActionAdd", GetType(Boolean))
                .Columns.Add("ActionEdit", GetType(Boolean))
                .Columns.Add("ActionDelete", GetType(Boolean))
                .Columns.Add("ActionPrint", GetType(Boolean))
                .Columns.Add("ActionImport", GetType(Boolean))
                .Columns.Add("ActionExport", GetType(Boolean))
                .Columns.Add("ActionFind", GetType(Boolean))
                .Columns.Add("ActionCheck", GetType(Boolean))
                .Columns.Add("ActionUncheck", GetType(Boolean))
                .Columns.Add("ActionComplete", GetType(Boolean))
            End With

            Dim newRow As DataRow
            For i As Integer = 0 To Me.dgv1.Rows.Count - 1
                newRow = nTable.NewRow

                newRow("GroupID") = GroupID
                newRow("ModeID") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ModeID").Value), False, Me.dgv1.Rows(i).Cells("ModeID").Value)
                newRow("ActionSee") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionSee").Value), False, Me.dgv1.Rows(i).Cells("ActionSee").Value)
                newRow("ActionEnable") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionEnable").Value), False, Me.dgv1.Rows(i).Cells("ActionEnable").Value)
                newRow("ActionBrowser") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionBrowser").Value), False, Me.dgv1.Rows(i).Cells("ActionBrowser").Value)
                newRow("ActionAdd") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionAdd").Value), False, Me.dgv1.Rows(i).Cells("ActionAdd").Value)
                newRow("ActionEdit") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionEdit").Value), False, Me.dgv1.Rows(i).Cells("ActionEdit").Value)
                newRow("ActionDelete") = IIf((Me.dgv1.Rows(i).Cells("ActionDelete").Value Is Nothing), False, Me.dgv1.Rows(i).Cells("ActionDelete").Value)
                newRow("ActionPrint") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionPrint").Value), False, Me.dgv1.Rows(i).Cells("ActionPrint").Value)
                newRow("ActionImport") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionImport").Value), False, Me.dgv1.Rows(i).Cells("ActionImport").Value)
                newRow("ActionExport") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionExport").Value), False, Me.dgv1.Rows(i).Cells("ActionExport").Value)
                newRow("ActionFind") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionFind").Value), False, Me.dgv1.Rows(i).Cells("ActionFind").Value)
                newRow("ActionCheck") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionCheck").Value), False, Me.dgv1.Rows(i).Cells("ActionCheck").Value)
                newRow("ActionUncheck") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionUncheck").Value), False, Me.dgv1.Rows(i).Cells("ActionUncheck").Value)
                newRow("ActionComplete") = IIf(IsDBNull(Me.dgv1.Rows(i).Cells("ActionComplete").Value), False, Me.dgv1.Rows(i).Cells("ActionComplete").Value)
                nTable.Rows.Add(newRow)
            Next
            nTable.AcceptChanges()
            Return nTable

        Catch ex As Exception
            MsgBox("ReturnTable" & ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Return Nothing
        End Try

    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function BtnActionSave() As Boolean

        Dim sqlConn As New SqlConnection
        Dim sqlcmd As New SqlCommand
        Dim SqlBulk As SqlClient.SqlBulkCopy

        sqlConn = GetConn()
        If sqlConn.State = ConnectionState.Closed Then
            sqlConn.Open()
        End If
        Dim transqlserver As SqlTransaction = sqlConn.BeginTransaction
        Try

            Dim sql As String = ""
            Dim ID As String = ""
            Dim GroupId As Integer

            Dim GroupNo As String = Me.txtGroupNo.Text.Replace(",", "").Replace("'", "")
            Dim GroupName As String = Me.txtGroupName.Text.Replace(",", "").Replace("'", "")
            Dim txtStartDate As String = ""
            Dim txtEndDate As String = ""
            Dim txtRemark As String = Me.txtRemark.Text.Replace(",", "").Replace("'", "")
            Dim Status As Integer = IIf(Me.CBStatus.Checked, 1, 0)
            Dim ModeName As String = ""

            If ActionInitialize() = False Then Exit Function

            txtStartDate = Me.txtStartDate.Text
            txtEndDate = Me.txtEndDate.Text

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                sql = "Insert Into Sys_Group(GroupCode,GroupName,GroupStartDate,GroupEndDate,GroupStatus,GroupRemark,BuildPerson,BuildComputer) values('"
                sql += GroupNo & "','" & GroupName & "','" & txtStartDate & "','" & txtEndDate & "'," & Status & ",'" & txtRemark & "','" & PubUserName & "','" & My.Computer.Name & "');"
                'sql += " select @@IDENTITY as GroupID"
                With sqlcmd
                    .Connection = sqlConn
                    .Transaction = transqlserver
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With

                Dim ResTable As DataTable = ReturnTable(GroupNo)
                SqlBulk = New SqlClient.SqlBulkCopy(sqlConn, SqlBulkCopyOptions.Default, transqlserver)
                SqlBulk.DestinationTableName = "Sys_Action"
                SqlBulk.BulkCopyTimeout = 100
                SqlBulk.WriteToServer(ResTable)

                AddToDataSet(GroupId, GroupNo, GroupName, txtStartDate, txtEndDate, ModeName, txtRemark, Status)
                AddToTreeView(Me.TreeView1.Nodes(0), GroupName, GroupNo)
                ResTable.Dispose()
            ElseIf ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then

                sql = "Update Sys_Group Set GroupName='" & GroupName & "',GroupStartDate='" & txtStartDate & "',GroupEndDate='" & txtEndDate & "',GroupStatus=" & Status & ",GroupRemark='" & txtRemark & "',UpdateDate=getdate(),UpdatePerson='" & PubUserName & "',UpdateComputer='"
                sql += My.Computer.Name & "' where GroupCode='" & Me.TreeView1.SelectedNode.Tag & "';"
                sql += "delete Sys_Action where GroupID='" & Me.TreeView1.SelectedNode.Tag.ToString & "'"

                With sqlcmd
                    .Connection = sqlConn
                    .Transaction = transqlserver
                    .CommandType = CommandType.Text
                    .CommandText = sql
                    .ExecuteNonQuery()
                End With

                Dim ResTable As DataTable = ReturnTable(GroupNo)
                SqlBulk = New SqlClient.SqlBulkCopy(sqlConn, SqlBulkCopyOptions.Default, transqlserver)
                SqlBulk.DestinationTableName = "Sys_Action"
                SqlBulk.BulkCopyTimeout = 100
                SqlBulk.WriteToServer(ResTable)

                EditToTreeView(Me.TreeView1.SelectedNode.Tag.ToString, GroupName, txtStartDate, txtEndDate, ModeName, txtRemark, Status)
                Me.TreeView1.SelectedNode.Text = GroupName
                ResTable.Dispose()
            End If
            transqlserver.Commit()
            sqlConn.Close()
            MsgBox(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.resources.Addsuccess, My.resources.EditSuccess), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            SetEnable(False)
            bFind = True
            MainForm.BtnEnable(bFind)
            GetToolEnable()
            Return True
        Catch ex As Exception
            transqlserver.Rollback()
            MsgBox("BtnActionSave:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            sqlConn.Close()
            sqlcmd.Dispose()
            SqlBulk.Close()
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <param name="GroupNo"></param>
    ''' <param name="GroupName"></param>
    ''' <param name="StartDate"></param>
    ''' <param name="EndDate"></param>
    ''' <param name="ModeName"></param>
    ''' <param name="Remark"></param>
    ''' <param name="Status"></param>
    ''' <remarks></remarks>
    Private Sub AddToDataSet(ByVal ID As Integer, ByVal GroupNo As String, ByVal GroupName As String, ByVal StartDate As String, ByVal EndDate As String, ByVal ModeName As String, ByVal Remark As String, ByVal Status As Integer)
        Try
            If mDataSet Is Nothing Then Exit Sub
            Dim mRow As DataRow = mDataSet.Tables(0).NewRow
            'mRow("ID") = ID
            mRow("GroupCode") = GroupNo
            mRow("GroupName") = GroupName
            mRow("GroupStartDate") = StartDate
            mRow("GroupEndDate") = EndDate
            mRow("GroupRemark") = Remark
            mRow("GroupStatus") = Status
            mDataSet.Tables(0).Rows.Add(mRow)
            mDataSet.AcceptChanges()
        Catch ex As Exception
            MsgBox("AddToDataSet:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="TreeNode"></param>
    ''' <param name="NodeText"></param>
    ''' <param name="NodeTag"></param>
    ''' <remarks></remarks>
    Private Sub AddToTreeView(ByVal TreeNode As TreeNode, ByVal NodeText As String, ByVal NodeTag As String)
        Try
            Dim mNode As New TreeNode
            mNode.Text = NodeText
            mNode.Tag = NodeTag
            mNode.ImageIndex = 1
            mNode.SelectedImageIndex = 2
            TreeNode.Nodes.Add(mNode)
        Catch ex As Exception
            MsgBox("AddToTreeView:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <param name="GroupName"></param>
    ''' <param name="StartDate"></param>
    ''' <param name="EndDate"></param>
    ''' <param name="ModeName"></param>
    ''' <param name="Remark"></param>
    ''' <param name="Status"></param>
    ''' <remarks></remarks>
    Private Sub EditToTreeView(ByVal GroupCode As String, ByVal GroupName As String, ByVal StartDate As String, ByVal EndDate As String, ByVal ModeName As String, ByVal Remark As String, ByVal Status As Integer)
        Try
            Dim Rows() As DataRow = mDataSet.Tables(0).Select("GroupCode='" & GroupCode & "'")
            If Rows.Length > 0 Then
                For Each mRow As DataRow In Rows
                    mRow("GroupName") = GroupName
                    mRow("GroupStartDate") = StartDate
                    mRow("GroupEndDate") = EndDate
                    mRow("GroupRemark") = Remark
                    mRow("GroupStatus") = Status
                Next
            End If
            mDataSet.AcceptChanges()
        Catch ex As Exception
            MsgBox("EditToTreeView:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function DeleteFromDataSet(ByVal ID As String) As Boolean
        Dim sql As String
        Try
            If IsExistRecord(ID) = True Then
                MsgBox("該組別下存在用戶，請先刪除用戶再刪除組別！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Return False
            End If
            sql = "Delete Sys_Group where GroupCode='" & ID & "'; Delete dbo.Sys_Action where GroupID='" & ID & "'"
            ReturnObject(sql)
            Me.TreeView1.SelectedNode.Remove()
            Return True

        Catch ex As Exception
            MsgBox("DeleteFromDataSet:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function IsExistRecord(ByVal ID As String) As Boolean
        Dim sql As String
        Dim mDS As New DataSet
        Try
            sql = "select * from dbo.Sys_UserManage where GroupID='" & ID & "'"
            mDS = ReturnDataset(sql)
            If mDS.Tables(0).Rows.Count > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            MsgBox("IsExistRecord:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="ID"></param>
    ''' <remarks></remarks>
    Private Sub FillText(ByVal ID As String)

        Dim sql As String = ""
        Dim nTable As New DataTable
        Try
            Dim Rows() As DataRow = mDataSet.Tables(0).Select("GroupCode='" & ID & "'")
            ClearText()
            If Rows.Length > 0 Then

                For Each mRow As DataRow In Rows
                    Me.txtGroupNo.Text = mRow("GroupCode") & ""
                    Me.txtGroupName.Text = mRow("GroupName") & ""
                    Me.txtStartDate.Text = mRow("GroupStartDate") & ""
                    Me.txtEndDate.Text = mRow("GroupEndDate") & ""
                    Me.txtRemark.Text = mRow("GroupRemark") & ""
                    Me.CBStatus.Checked = mRow("GroupStatus")
                Next
                sql = "select b.ModeID,a.ModeName,ActionSee, ActionEnable, ActionBrowser, ActionAdd, ActionEdit, ActionDelete, ActionPrint, ActionImport, ActionExport, ActionFind, ActionCheck, ActionUncheck, ActionComplete "
                sql += "from Sys_ModeDefine a inner join dbo.Sys_Action b on a.ModeNo=b.ModeId where b.GroupID='" & ID & "'"
                nTable = ReturnDataTable(sql)
                Me.dgv1.DataSource = nTable
                Me.dgv1.Columns(0).Visible = False
            End If
        Catch ex As Exception
            MsgBox("FillText:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            nTable.Dispose()
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub FormatGrid()
        Try
            GridDs = New DataSet
            Dim mTable As New DataTable
            With mTable
                .Columns.Add("ModeNo")
                .Columns.Add("ModeName", GetType(String))
                .Columns.Add("ActionSee", GetType(Boolean))
                .Columns.Add("ActionEnable", GetType(Boolean))
                .Columns.Add("ActionBrowser", GetType(Boolean))
                .Columns.Add("ActionAdd", GetType(Boolean))
                .Columns.Add("ActionEdit", GetType(Boolean))
                .Columns.Add("ActionDelete", GetType(Boolean))
                .Columns.Add("ActionImport", GetType(Boolean))
                .Columns.Add("ActionExport", GetType(Boolean))
                .Columns.Add("ActionPrint", GetType(Boolean))
                .Columns.Add("ActionFind", GetType(Boolean))
                .Columns.Add("ActionCheck", GetType(Boolean))
                .Columns.Add("ActionUncheck", GetType(Boolean))
                .Columns.Add("ActionComplete", GetType(Boolean))
            End With
            GridDs.Tables.Add(mTable)
            Me.dgv1.DataSource = GridDs.Tables(0)
            With Me.dgv1
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = GetGridCaption().Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                Next
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .BackgroundColor = Color.AliceBlue
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .AllowUserToResizeColumns = True
                .AllowUserToResizeRows = True
                .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells
                .Columns(1).Frozen = True
                .Columns("ModeNo").Visible = False
            End With

        Catch ex As Exception
            MsgBox("FormatGrid:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetGridCaption() As Hashtable
        Try
            Dim mHash As New Hashtable
            With mHash
                .Add("ModeNo", "ModeNo")
                .Add("ModeName", "模塊名稱")
                .Add("ActionSee", "可見")
                .Add("ActionEnable", "可用")
                .Add("ActionBrowser", "瀏覽")
                .Add("ActionAdd", "新增")
                .Add("ActionEdit", "修改")
                .Add("ActionDelete", "刪除")
                .Add("ActionImport", "導出")
                .Add("ActionExport", "導入")
                .Add("ActionPrint", "打印")
                .Add("ActionFind", "查找")
                .Add("ActionCheck", "審核")
                .Add("ActionUncheck", "反審核")
                .Add("ActionComplete", "完成")
            End With
            Return mHash
        Catch ex As Exception
            Return Nothing
            MsgBox("GetGridCaption:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadGridData()
        Try
            Dim sql As String = ""
            sql = "select ParentID,ModeName,ActionSee=0,ActionEnable=0,ActionBrowser=0,ActionAdd=0,ActionEdit=0,"
            sql += "ActionDelete=0,ActionImport=0,ActionExport=0,ActionPrint=0,ActionFind=0,ActionCheck=0,ActionUncheck=0,ActionComplete=0 from dbo.Sys_ModeDefine where ParentId <>0 order by ParentId"
            GridDs = ReturnDataset(sql)
            Me.dgv1.DataSource = GridDs.Tables(0)
            Me.dgv1.Columns("ParentId").Visible = False
            For i As Integer = 0 To Me.dgv1.Columns.Count - 1
                If Me.dgv1.Columns(i).Name = "ActionSee" Then
                    Me.dgv1.Columns(i).ReadOnly = False
                Else
                    Me.dgv1.Columns(i).ReadOnly = True
                End If
            Next


        Catch ex As Exception
            MsgBox("LoadGridData:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadModeName(ByVal mBool As Boolean)
        Dim mTable As New DataTable
        Try
            Dim sql As String = ""

            If mBool = True Then
                sql = "select ModeNo as ModeID,ModeName,ActionSee=0,ActionEnable=0,ActionBrowser=0,ActionAdd=0,ActionEdit=0,ActionDelete=0,ActionImport=0,ActionExport=0,ActionPrint=0,ActionFind=0,ActionCheck=0,ActionUncheck=0,ActionComplete=0 from dbo.Sys_ModeDefine where ParentID<>0 order by ParentId"
            Else
                sql = "select ModeNo as ModeID,ModeName,ActionSee=0,ActionEnable=0,ActionBrowser=0,ActionAdd=0,ActionEdit=0,ActionDelete=0,ActionImport=0,ActionExport=0,ActionPrint=0,ActionFind=0,ActionCheck=0,ActionUncheck=0,ActionComplete=0 from dbo.Sys_ModeDefine where ParentID<>0"
                sql += "and ModeNo not in (select ModeID from dbo.Sys_Action where GroupID='" & Me.TreeView1.SelectedNode.Tag.ToString & "')"
            End If
            mTable = ReturnDataTable(sql)

            If mBool = True Then
                Me.dgv1.DataSource = mTable
                Me.dgv1.Columns("ModeID").Visible = False
            Else
                If mTable.Rows.Count > 0 Then
                    Dim GridTable As DataTable = CType(Me.dgv1.DataSource, DataTable)
                    Dim NewRow As DataRow
                    For Each mRow As DataRow In mTable.Rows
                        NewRow = GridTable.NewRow
                        NewRow("ModeID") = mRow("ModeID")
                        NewRow("ModeName") = mRow("ModeName")
                        NewRow("ActionSee") = mRow("ActionSee")
                        NewRow("ActionEnable") = mRow("ActionEnable")
                        NewRow("ActionBrowser") = mRow("ActionBrowser")
                        NewRow("ActionAdd") = mRow("ActionAdd")
                        NewRow("ActionEdit") = mRow("ActionEdit")
                        NewRow("ActionDelete") = mRow("ActionDelete")
                        NewRow("ActionImport") = mRow("ActionImport")
                        NewRow("ActionExport") = mRow("ActionExport")
                        NewRow("ActionPrint") = mRow("ActionPrint")
                        NewRow("ActionFind") = mRow("ActionFind")
                        NewRow("ActionCheck") = mRow("ActionCheck")
                        NewRow("ActionUncheck") = mRow("ActionUncheck")
                        NewRow("ActionComplete") = mRow("ActionComplete")
                        GridTable.Rows.Add(NewRow)
                    Next
                    GridTable.AcceptChanges()
                    Me.dgv1.DataSource = GridTable
                    GridTable.Dispose()
                End If
            End If

        Catch ex As Exception
            MsgBox("LoadModeName:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable.Dispose()
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetToolEnable()
        Try
            With MainForm
                .ToolStripAdd.Enabled = AddEnable
                .ToolStripEdit.Enabled = EditEnable
                .ToolStripDelete.Enabled = DeleteEnable
                .ToolStripPrint.Enabled = PrintEnable
                .ToolStripImport.Enabled = ImportEnable
                .ToolStripExport.Enabled = ExportEnable
                .ToolStripFind.Enabled = FindEnable
                .ToolStripCheck.Enabled = CheckEnable
                .ToolStripUnCheck.Enabled = UncheckEnable
                .ToolStripComplete.Enabled = CompleteEnable
                .AddToolStripMenuItem.Enabled = .ToolStripAdd.Enabled
                .Edit1ToolStripMenuItem.Enabled = .ToolStripEdit.Enabled
                .DelToolStripMenuItem.Enabled = .ToolStripDelete.Enabled
                .SaveToolStripMenuItem.Enabled = .ToolStripSave.Enabled
                .CancelToolStripMenuItem.Enabled = .ToolStripCancel.Enabled
                .PrintToolStripMenuItem.Enabled = .ToolStripPrint.Enabled
                .ExportToolStripMenuItem.Enabled = .ToolStripImport.Enabled
                .ImportToolStripMenuItem.Enabled = .ToolStripExport.Enabled
                .FilterToolStripMenuItem.Enabled = .ToolStripFilter.Enabled
            End With

        Catch ex As Exception
            MsgBox("GetToolEnable:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub
#End Region


#Region "interface define"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        Try
            If AddEnable = False Then
                MessageBox.Show(My.resources.notallowadd, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
            ClearText()
            SetEnable(True)
            MainForm.BtnEnable(False)
            Me.txtGroupNo.Focus()
            LoadModeName(True)
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            ClearText()
            SetEnable(False)
            If Me.TreeView1.SelectedNode.Tag.ToString = "All" Then
                bFind = False
            Else
                bFind = True
                FillText(Me.TreeView1.SelectedNode.Tag)
            End If
            MainForm.BtnEnable(bFind)
            GetToolEnable()
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Dim ID As String
        Try
            If DeleteEnable = False Then
                MessageBox.Show(My.resources.NotAllowdelete, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            ID = SelectNode.Tag.ToString
            If MsgBox(My.resources.ConfirmDelete, MsgBoxStyle.Information + MsgBoxStyle.OkCancel, Msg.CompanyName) = MsgBoxResult.Ok Then
                If DeleteFromDataSet(ID) = True Then
                    MsgBox(My.resources.DeleteSuccess, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                End If
            End If
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            GetToolEnable()
        Catch ex As Exception
            MsgBox("ActionDelete:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        Try
            If EditEnable = False Then
                MessageBox.Show(My.resources.NotAllowEidt, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
            SetEnable(True)
            MainForm.BtnEnable(False)
            LoadModeName(False)
            Me.txtGroupNo.Enabled = False
            Me.txtGroupName.Focus()
            Me.txtGroupName.SelectAll()
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport
        Try
            App.ExportToExcel(Me.dgv1, Msg.CompanyName)
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter
        Try

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst
        Try

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast
        Try

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext
        Try

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious
        Try

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport
        Try

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize
        Try
            Dim sql As String = ""
            Dim m_ds As New DataSet
            If Me.txtGroupNo.Text.Length <= 0 Then
                MsgBox(Msg.NotNullGroupNo, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtGroupNo.Focus()
                Return False
            End If
            If Me.txtGroupName.Text.Length <= 0 Then
                MsgBox(Msg.NotNullGroupName, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtGroupName.Focus()
                Return False
            End If
            If Me.txtStartDate.Text = "" Then
                MsgBox(Msg.NotNullStartDate, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtStartDate.Focus()
                SendKeys.Send("{F4}")
                Return False
            Else
                If Not IsDate(Me.txtStartDate.Text) Then
                    MsgBox(Msg.NotValidValidDate, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    Me.txtStartDate.Focus()
                    SendKeys.Send("{F4}")
                    Return False
                End If
            End If
            If Me.txtEndDate.Text = "" Then
                MsgBox(Msg.NotNullEndDate, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtEndDate.Focus()
                SendKeys.Send("{F4}")
                Return False
            Else
                If Not IsDate(Me.txtEndDate.Text) Then
                    MsgBox(Msg.NotValidValidDate, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    Me.txtEndDate.Focus()
                    SendKeys.Send("{F4}")
                    Return False
                End If
            End If
            If CDate(Me.txtStartDate.Text) > CDate(Me.txtEndDate.Text) Then
                MsgBox(My.Resources.NotLargeEndDate, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtStartDate.Focus()
                SendKeys.Send("{F4}")
                Return False
            End If


            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                sql = "select * from Sys_PurviewManager where GroupNo='" & Me.txtGroupNo.Text.Replace(",", "").Replace("'", "") & "'"
                m_ds = ReturnDataset(sql)
                If m_ds.Tables(0).Rows.Count > 0 Then
                    MsgBox(My.Resources.ExistsameCode, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    Me.txtGroupNo.Focus()
                    Me.txtGroupNo.SelectAll()
                    Return False
                End If
            End If
            Return True
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint
        Try

        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        Try
            If BtnActionSave() = True Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Function

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect
        Try

        Catch ex As Exception
            Throw ex
        End Try
    End Function

#End Region


#Region "Windows Event"

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Frm_SYSUserpurview_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            LoadTreeView(Me.TreeView1)
            BindGridView()
            ClearText()
            SetEnable(False)
        Catch ex As Exception
            MsgBox("Frm_SYSUserpurview_Load:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtGroupNo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtGroupNo.KeyDown, txtGroupName.KeyDown, txtRemark.KeyDown, CBStatus.KeyDown
        Try
            If e.KeyCode = Keys.Shift Then
                e.Handled = True
                If e.KeyCode = Keys.Enter Then
                    SendKeys.Send("+{TAB}")
                    SendKeys.Send("{F4}")
                End If
            Else
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        SendKeys.Send("{TAB}")
                        SendKeys.Send("{F4}")
                End Select
            End If
        Catch ex As Exception
            MsgBox("txtGroupNo_KeyDown:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Try
            MainForm.ToolStripCancel.PerformClick()
            ' MainForm.ToolStripCancel_Click(Nothing, Nothing)
        Catch ex As Exception
            MsgBox("BtnCancel_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BtnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnOK.Click
        Try
            MainForm.ToolStripSave.PerformClick()
            'MainForm.ToolStripSave_Click(Nothing, Nothing)
        Catch ex As Exception
            MsgBox("BtnOK_Click:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Try
            SelectNode = e.Node
            If e.Action = TreeViewAction.ByKeyboard Or e.Node.Tag.ToString = "All" Then
                bFind = False
            Else
                FillText(e.Node.Tag.ToString)
                bFind = True
                MainForm.BtnEnable(bFind)
                GetToolEnable()
            End If
        Catch ex As Exception
            MsgBox("TreeView1_AfterSelect:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub checkboxAll_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles checkboxAll.CheckedChanged

        Try

            If Me.checkboxAll.Checked = True Then
                For i As Int16 = 0 To Me.dgv1.Rows.Count - 1

                    For j As Integer = 0 To Me.dgv1.Rows(i).Cells.Count - 1

                        If Me.dgv1.Columns(j).GetType.Name = "DataGridViewCheckBoxColumn" Then
                            Me.dgv1.Rows(i).Cells(j).Value = 1
                        End If
                    Next
                Next
            Else
                For i As Int16 = 0 To Me.dgv1.Rows.Count - 1

                    For j As Integer = 0 To Me.dgv1.Rows(i).Cells.Count - 1

                        If Me.dgv1.Columns(j).GetType.Name = "DataGridViewCheckBoxColumn" Then
                            Me.dgv1.Rows(i).Cells(j).Value = 0
                        End If
                    Next
                Next
            End If
        Catch ex As Exception

        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub dgv1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv1.CellClick

        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then

                Me.dgv1.CommitEdit(DataGridViewDataErrorContexts.Commit)
                If e.ColumnIndex = 2 Then

                    If Me.dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value Is DBNull.Value Then

                        Me.dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 1
                        For i As Integer = 3 To Me.dgv1.Columns.Count - 1
                            Me.dgv1.Rows(e.RowIndex).Cells(i).Style.BackColor = Color.White
                            Me.dgv1.Rows(e.RowIndex).Cells(i).ReadOnly = False
                            Me.dgv1.Rows(e.RowIndex).Cells(i).Value = 1
                        Next
                    Else
                        If Me.dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0 Then
                            Me.dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 1
                            For i As Integer = 3 To Me.dgv1.Columns.Count - 1
                                Me.dgv1.Rows(e.RowIndex).Cells(i).Style.BackColor = Color.White
                                Me.dgv1.Rows(e.RowIndex).Cells(i).ReadOnly = False
                                Me.dgv1.Rows(e.RowIndex).Cells(i).Value = 1
                            Next
                        Else
                            Me.dgv1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = 0
                            For i As Integer = 3 To Me.dgv1.Columns.Count - 1
                                Me.dgv1.Rows(e.RowIndex).Cells(i).Style.BackColor = Color.Silver
                                Me.dgv1.Rows(e.RowIndex).Cells(i).ReadOnly = True
                                Me.dgv1.Rows(e.RowIndex).Cells(i).Value = 0
                            Next
                        End If

                    End If
                End If

            End If

        Catch ex As Exception
            MsgBox("dgv1_CellClick:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub cbActionSee_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbActionSee.CheckedChanged, cbActionEnable.CheckedChanged, cbActionBrowser.CheckedChanged, cbAdd.CheckedChanged, cbEdit.CheckedChanged, _
                                         cbDelete.CheckedChanged, cbPrint.CheckedChanged, cbImport.CheckedChanged, cbExport.CheckedChanged, cbFind.CheckedChanged, cbCheck.CheckedChanged, cbUnCheck.CheckedChanged

        Try

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then

                Dim ColumnName As String = ""

                Select Case sender.name.ToString.ToLower
                    Case "cbActionSee".ToLower
                        ColumnName = "ActionSee"
                    Case "cbActionEnable".ToLower
                        ColumnName = "ActionEnable"
                    Case "cbActionBrowser".ToLower
                        ColumnName = "ActionBrowser"
                    Case "cbAdd".ToLower
                        ColumnName = "ActionAdd"
                    Case "cbEdit".ToLower
                        ColumnName = "ActionEdit"
                    Case "cbDelete".ToLower
                        ColumnName = "ActionDelete"
                    Case "cbPrint".ToLower
                        ColumnName = "ActionPrint"
                    Case "cbImport".ToLower
                        ColumnName = "ActionImport"
                    Case "cbExport".ToLower
                        ColumnName = "ActionExport"
                    Case "cbFind".ToLower
                        ColumnName = "ActionFind"
                    Case "cbCheck".ToLower
                        ColumnName = "ActionCheck"
                    Case "cbUnCheck".ToLower
                        ColumnName = "ActionUncheck"
                End Select

                For i As Integer = 0 To Me.dgv1.Rows.Count - 1
                    Me.dgv1.Rows(i).Cells(ColumnName).Value = IIf(sender.Checked, 1, 0)
                Next

            End If

        Catch ex As Exception
            MsgBox("CheckedChanged:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub


#End Region


#Region "Tabpage2"

    Private Sub BindGridView()

        Dim mTable As New DataTable

        Try

            With mTable
                .Columns.Add("ModeID")
                .Columns.Add("ModeName", GetType(String))
                .Columns.Add("ActionSee", GetType(Boolean))
                .Columns.Add("ActionEnable", GetType(Boolean))
                .Columns.Add("ActionBrowser", GetType(Boolean))
                .Columns.Add("ActionAdd", GetType(Boolean))
                .Columns.Add("ActionEdit", GetType(Boolean))
                .Columns.Add("ActionDelete", GetType(Boolean))
                .Columns.Add("ActionPrint", GetType(Boolean))
                .Columns.Add("ActionImport", GetType(Boolean))
                .Columns.Add("ActionExport", GetType(Boolean))
                .Columns.Add("ActionFind", GetType(Boolean))
                .Columns.Add("ActionCheck", GetType(Boolean))
                .Columns.Add("ActionUncheck", GetType(Boolean))
                .Columns.Add("ActionComplete", GetType(Boolean))
            End With

            Me.dgv1.DataSource = mTable

            With Me.dgv1
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = ReturnHash.Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                    If i <> 1 Then
                        .Columns(i).Width = 60
                    End If
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
                .MultiSelect = False
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                .Columns("ModeID").Visible = False
                '.Columns("ModeName").Frozen = True
            End With

        Catch ex As Exception
            MsgBox("BindGridView:" & ex.Message, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    Private Function ReturnHash() As Hashtable
        Try
            Dim mHash As New Hashtable
            With mHash
                .Add("ModeID", "ModeID")
                .Add("ModeName", "模塊名稱")
                .Add("ActionSee", "可見")
                .Add("ActionEnable", "可用")
                .Add("ActionBrowser", "瀏覽")
                .Add("ActionAdd", "新增")
                .Add("ActionEdit", "修改")
                .Add("ActionDelete", "刪除")
                .Add("ActionPrint", "打印")
                .Add("ActionImport", "導出")
                .Add("ActionExport", "導入")
                .Add("ActionFind", "查找")
                .Add("ActionCheck", "審核")
                .Add("ActionUncheck", "反審核")
                .Add("ActionComplete", "完成")
            End With
            Return mHash
        Catch ex As Exception
            MsgBox("BindHash:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Return Nothing
        End Try
    End Function
#End Region




    Private Sub Panel4_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel4.Paint

    End Sub
End Class
